/*
 * NetSender.cpp
 *
 *  Created on: 2011-04-17
 *      Author: jacek
 */

#include "NetSender.h"

NetSender::NetSender() {
	senderSocket=NULL;

}
NetSender::NetSender(Socket& senderSocket){
	this->senderSocket=&senderSocket;

}
NetSender::~NetSender() {
	senderSocket=NULL;
}

void NetSender::ConvertEventHostToNet(Event & event)
{
	event.type = htonl(event.type);
    event.destination = htonl(event.destination);
    event.sender = htonl(event.sender);
}

void NetSender::Updata(Event event)
{
	if(IsNetEvent(event.type)){
		ConvertEventHostToNet(event);

		package.buffor=reinterpret_cast<char*>(&event);

		try{
			senderSocket->Send(package);
		}
		catch (SocketException e) {
			Event error;
			error.destination=12;
			error.sender=12;
			error.type=999;
			parent->SendEvent(error);
		}
	}
}
void NetSender::Intialization()
{
	package.buffor=NULL;
	package.size=sizeof(Event);
}
void NetSender::Dispose()
{
	senderSocket=NULL;
}
bool NetSender::IsNetEvent(unsigned int type){

	return (type & 0x80000000)==NET_E;
}
void NetSender::SendEvent(Event event){
	parent->SendEvent(event);
}

